#include <stdio.h>
#include "patient.h"
#include <string.h>
#include <stdlib.h>


void assginmentPatient(Patient* a, Patient* b)
{
    strcpy(a->name, b->name);
    a->age = b->age;
    strcpy(a->idetify_card, b->idetify_card);
}
int judgeInputPatient(Patient* a)     //判断输入函数，合法返回1并将输入对象进行赋值，中途退出输入2
{
    int d;
    while (true)
    {
        printf("是否需要回到上一级操作:1.是；2.不是\n");
        scanf("%d", &d);
        getchar();
        if (d == 1)
        {
            return 2;
        }
        printf("请输入您的年龄\n");
        if (judgeYear(a) == 0)
        {
            printf("您输入的年龄非法，请重新输入\n");
            continue;
        }
        printf("请输入您的身份证号\n");
        if (judgeIdentifyCard(a) == 0)
        {
            printf("您输入的身份证号非法，请重新输入\n");
            continue;
        }
        return 1;
    }
}
int judgeYear(Patient* a)                  //检查输入年龄是否合法，合法则将年龄赋值给patient对象成员age并输出1，不合法输出0（合法年龄范围：1-120）
{
    int year = 0;
    char number[4];
    char ch;
    int i = 0;
    scanf("%c", &ch);
    while (ch != '\n')
    {
        number[i] = ch;
        scanf("%c", &ch);
        i++;
        if (i == 3)
        {
            return 0;
        }
    }
    for (int j = i; j < 4; j++)
    {
        number[j] = '\0';
    }
    for (int i = 0; i < 3; i++)
    {
        if (('0' > number[i] || number[i] > '9') && number[i] != '\0')
        {
            return 0;
        }
    }
    for (int i = 0; i < 3; i++)
    {
        if (number[i] == '\0')
        {
            break;
        }
        year = year * 10;
        year += (number[i] - '0');
    }
    if (year > 120 || year == 0)
    {
        return 0;
    }
    a->age = year;
    return 1;
}
int judgeIdentifyCard(Patient* a)       //检查输入身份证的合法性，合法输出1并完成赋值操作，不合法输出0
{
    char idetifyCard[18];
    char ch;
    for (int i = 0; i < 17; i++)
    {
        scanf("%c", &ch);
        if ('0' > ch || ch > '9')
        {
            return 0;
        }
        else
            idetifyCard[i] = ch;
    }
    scanf("%c", &ch);
    getchar();
    if (('0' > ch || ch > '9') && (ch != 'X' && ch != 'x'))
    {
        return 0;
    }
    idetifyCard[17] = ch;
    strcpy(a->idetify_card, idetifyCard);
    return 1;
}
void displayPatient(Patient* a)
{
    printf("患者的姓名：%s\n", a->name);
    printf("患者的年龄：%d\n", a->age);
    printf("患者的身份证号：%s\n", a->idetify_card);
    printf("\n");
}

bool isPatientNodeListEmpty(PatientNode* List)
{
    if (List == NULL)
        return 1;
    else return 0;
}
void changePatientNode(PatientNode* a, Patient* Data)
{
    strcpy(a->data.name, Data->name);
    a->data.age = Data->age;
    strcpy(a->data.idetify_card, Data->idetify_card);
}
PatientNode* createPatientNode(Patient* Data)
{
    PatientNode* temp = (PatientNode*)malloc(sizeof(PatientNode));
    changePatientNode(temp, Data);
    temp->pre = temp->next = NULL;
    return temp;
}
int addPatientNode(PatientNode* List, Patient* Data)          //返回2表示用户拒绝输入，返回上一界面，返回1表示链表没有建立，返回0表示加入结点成功
{
    PatientNode* temp = List;
    PatientNode* p = NULL;
    Patient* data = (Patient*)malloc(sizeof(Patient));
    if (isPatientNodeListEmpty(List))
    {
        printf("ERROR:List hasn't been created！\n");
        return 0;
    }
    while (temp->next)
        temp = temp->next;
    if (judgeInputPatient(data) == 2)
    {
        return 2;
    }
    p = createPatientNode(data);
    temp->next = p;
    p->pre = temp;
    return 1;
}
int deletePatientNode(PatientNode* temp)   //返回1成功，0失败
{
    if (temp)
    {
        if (temp->next)
        {
            temp->pre->next = temp->next;
            temp->next->pre = temp->pre;
        }
        else
        {
            temp->pre->next = NULL;
        }
        free(temp);
        return 1;
    }
    else
    {
        printf("ERROR:No node like this OR No List!");
        return 0;
    }
}
PatientNode* findPatientNode(PatientNode* List, char idetifyCard[19])
{
    PatientNode* temp = List;
    if (isPatientNodeListEmpty(List))
    {
        printf("ERROR:List hasn't been created！\n");
        return 0;
    }
    while (temp && strcmp(temp->data.idetify_card,idetifyCard) != 0)
        temp = temp->next;
    if (temp)
        return temp;
    else
    {
        printf("您想寻找的病人信息不在这个数据库中");
        return 0;
    }
}
int cleanPatientNode(PatientNode* List)
{
    if (isPatientNodeListEmpty(List))
    {
        printf("ERROR:The List is empty!\n");
        return 0;
    }
    PatientNode* temp = List;
    while (temp->next)
    {
        free(List);
        temp = temp->next;
        List = temp;
    }
    free(List);
    return 1;
}
void displayPatientNode(PatientNode* List)
{
    if (isPatientNodeListEmpty(List))
    {
        printf("ERROR:The List is empty!\n");
        return;
    }
    PatientNode* temp = List;
    while (temp)
    {
        displayPatient(&(temp->data));
        temp = temp->next;
    }
}

PatientNode* createNullPatientNode()
{
    PatientNode* temp = (PatientNode*)malloc(sizeof(PatientNode));
    temp->pre = temp->next = NULL;
    return temp;
}

PatientNode* readAPatient(FILE* fp)
{
    if (!fp)
    {
        printf("ERROR:Fail to read file!\n");
        return 0;
    }

    PatientNode* temp = createNullPatientNode();
    fscanf(fp, "%s %d %s", temp->data.name, &temp->data.age, temp->data.idetify_card);
    return temp;
}

PatientNode* loadPatientList()
{
    FILE* fp = NULL;
    if ((fp = fopen("patient.json", "r")) == NULL)
    {
        printf("ERROR:Can't open patient database!\n");
        return 0;
    }
    fscanf(fp, "%d", &PatientNum);
    PatientNode* PatientList = NULL;
    PatientNode* temp = NULL;
    PatientList = temp = readAPatient(fp);
    int num = 1;                                                // 用于记录已经录入的数目
    while (!feof(fp) && num++ < PatientNum)
    {
        temp->next = readAPatient(fp);
        temp->next->pre = temp;
        temp = temp->next;
    }
    temp->next = NULL;
    fclose(fp);
    return PatientList;
}

bool writeAPatient(FILE* fp, PatientNode* patient)
{

    if (3 != fprintf(fp, "%s %d %s \n", patient->data.name, patient->data.age, patient->data.idetify_card))
    {
        printf("ERROR:Fail to Write");
        return 0;
    }
    else
    {
        return 1;
    }

}

bool writePatientList(PatientNode* PatientList)
{
    FILE* fp = NULL;
    if ((fp = fopen("patient.json", "w")) == 0)
    {
        printf("Can't open patient.json");
        return 0;
    }
    PatientNode* temp = PatientList;
    int num = 0;

    if (fprintf(fp, "%d\n", PatientNum) != 1)
    {
        printf("ERROR: Fail to Write Patient Num");
        return 0;
    }
    while (num++ < PatientNum && temp)
    {
        if (writeAPatient(fp, temp) == 0)
        {
            printf("ERROR: Fail to Write Patient NO.%d", num);
            return 0;
        }
    }
    fclose(fp);
    return 1;
}

